package com.yc.demo.config;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.core.date.DateUtil;

/**
 * <p>自定义 p6spy sql输出格式</p>
 *
 * @author YuanChilde
 * @date 2019-09-17 11:46
 * @version 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2019-09-17 11:46    YuanChilde     1.0        新增
 */
public class P6spySqlFormatConfig implements MessageFormattingStrategy {

    /**
     * 过滤掉定时任务的 SQL
     */
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        return StringUtils.isNotBlank(sql) ? DateUtil.now()
                + " | 耗时 " + elapsed + " ms | SQL 语句：" + StringUtils.LF + sql.replaceAll("[\\s]+", StringUtils.SPACE) + ";" : StringUtils.EMPTY;
    }
}
